package com.molihub.utils;

import com.baidu.aip.contentcensor.AipContentCensor;
import org.json.JSONObject;
import org.slf4j.Logger;

import javax.xml.soap.Text;

/**
 * @ClassName: TextCheckUtil
 * @Description: 内容文本审核类
 * @author: GGBOY
 * @date 2020/1/19 14:29
 * @Version: 1.0
 **/
public class TextCheckUtil {
    /**
     * 设置 APP_ID/AK/SK
     */
    public static final String APP_ID = "18317392";
    public static final String API_KEY = "3z5nUf20KyjGIDUvTaa6Mawn";
    public static final String SECRET_KEY = "gxktFTdOr6aSbUBtVB9Kcfb3HgGZ65eq";
    public static AipContentCensor client;

    private static Logger logger = LogUtils.getInstance(TextCheckUtil.class);

    static {
        // 初始化一个AipImageCensor
        client = new AipContentCensor(APP_ID, API_KEY, SECRET_KEY);
        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

    }


    /**
     * 对传递过来的文本内容进行审核，如果审核通过返回True，否则返回FALSE
     *
     * @param text 需要审核的文本
     * @return 审核结果
     * @author GGBOY
     * @date 2020/1/19
     */
    public static boolean check(String text) {
        JSONObject response = client.textCensorUserDefined(text);
        Integer result = (Integer) response.get("conclusionType");
        logger.info(response.toString());
        // 如果文本审核通过，即合规，则返回 true,否则返回 false
        // 1.合规，2.不合规，3.疑似，4.审核失败
        return 1 == result;
    }

}
